package co.edu.uniandes.mdd.project1.sample.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import co.edu.uniandes.mdd.project1.sample.domain.Enemy;
import co.edu.uniandes.mdd.project1.sample.domain.Item;
import co.edu.uniandes.mdd.project1.sample.domain.PassiveElement;
import co.edu.uniandes.mdd.project1.sample.domain.Player;

public class ElementAttributeManager {

	private PropertiesLoader loader = new PropertiesLoader();
	private String[][] labyrinth = null;

	public ElementAttributeManager(int height, int width) {
		BufferedReader reader = null;
		labyrinth = new String[height][width];

		try {
			reader = new BufferedReader(new FileReader("C:\\MDD\\runtime-project1wspc\\co.edu.uniandes.mdd.project1.sample\\src\\co\\edu\\uniandes\\mdd\\project1\\sample\\properties\\labyrinth.txt"));
			String line = null;
			int row = 0;

			while ((line = reader.readLine()) != null) {
				labyrinth[row] = line.split(";");

				row++;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Player loadPlayerAttributes() {
		Player player = new Player();

		player.setPositionX(Integer.parseInt(loader
				.getPacmanProperty("pacmanX")));
		player.setPositionY(Integer.parseInt(loader
				.getPacmanProperty("pacmanY")));
		player.setScore(Integer.parseInt(loader.getPacmanProperty("pacmanScore")));
		player.setVisible(Boolean.parseBoolean(loader
				.getPacmanProperty("pacmanVisible")));
		player.setImage(loader.getPacmanProperty("pacmanImage"));

		return player;
	}

	public Enemy[] loadEnemiesAttributes() {
		Enemy[] enemies = new Enemy[Integer.parseInt(loader
				.getGhostProperty("enemies"))];

		for (int i = 0; i < enemies.length; i++) {
			enemies[i] = new Enemy();
			enemies[i].setImage(loader.getGhostProperty(i + ".ghostImage"));
			enemies[i].setPositionX(Integer.parseInt(loader.getGhostProperty(i
					+ ".ghostX")));
			enemies[i].setPositionY(Integer.parseInt(loader.getGhostProperty(i
					+ ".ghostY")));
			enemies[i].setVisible(Boolean.parseBoolean(loader
					.getGhostProperty(i + ".ghostVisible")));
			enemies[i].setPosition(i);
		}

		return enemies;
	}

	public PassiveElement[] loadLabyrinth() {
		Collection<PassiveElement> blocks = new ArrayList<PassiveElement>();

		for (int i = 0; i < this.labyrinth[0].length; i++) {
			for (int j = 0; j < this.labyrinth.length; j++) {
				if (labyrinth[i][j].equals("1")) {
					PassiveElement block = new PassiveElement();
					block.setImage(loader.getBlockProperty("blockImage"));
					block.setVisible(Boolean.parseBoolean(loader
							.getBlockProperty("blockVisible")));
					block.setPositionX(j);
					block.setPositionY(i);

					blocks.add(block);
				}
			}
		}
		PassiveElement[] passiveElements = new PassiveElement[blocks.size()];
		passiveElements = (PassiveElement[]) blocks.toArray(passiveElements);
		return passiveElements;
	}

	public Item[] loadDots() {
		Collection<Item> dots = new ArrayList<Item>();
		int cont = 0;
		for (int i = 0; i < this.labyrinth[0].length; i++) {
			for (int j = 0; j < this.labyrinth.length; j++) {
				if (labyrinth[i][j].equals("2")) {
					Item dot = new Item();
					dot.setImage(loader.getDotProperty("pelletImage"));
					dot.setVisible(Boolean.parseBoolean(loader
							.getDotProperty("pelletVisible")));
					dot.setPositionX(j);
					dot.setPositionY(i);
					dot.setPosition(cont++);
					dots.add(dot);
				}
			}
		}
		Item[] items = new Item[dots.size()];
		items = (Item[]) dots.toArray(items);
		return items;
	}

	public Collection<Item> loadPowerPellets() {
		Collection<Item> powerPellets = new ArrayList<Item>();

		for (int i = 0; i < this.labyrinth[0].length; i++) {
			for (int j = 0; j < this.labyrinth.length; j++) {
				if (labyrinth[i][j].equals("3")) {
					Item powerPellet = new Item();
					powerPellet
							.setImage(loader.getPowerPelletProperty("powerPelletImage"));
					powerPellet.setVisible(Boolean.parseBoolean(loader
							.getPowerPelletProperty("powerPelletVisible")));
					powerPellet.setPositionX(j);
					powerPellet.setPositionY(i);
					powerPellets.add(powerPellet);
				}
			}
		}
		return powerPellets;
	}

	// private void imprimirMatrix(){
	// for (int i = 0; i < 12; i++) {
	// for (int j = 0; j < 12; j++) {
	// System.out.print(this.labyrinth[i][j]);
	// System.out.print("["+i+","+j+"]");
	// }
	// System.out.println();
	// }
	// }
}
